<template>
  <div class="app-main-container">
    <div class="doumee-filter">
      <el-form inline>
        <el-form-item label="适用项目:">
          <el-select v-model="selectProNmae" placeholder="全部项目" clearable @change="conditionChange()">
            <el-option
              v-for="item in project.projectList"
              :key="item.id"
              :label="item.projectName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="门票日期:">
          <el-date-picker
            v-model="param.date"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            class="data-picker-style"
            :clearable="false"
            :editable="false"
            @change="conditionChange()"
          />
        </el-form-item>
      </el-form>

    </div>
    <div class="top-speace saleTickets">
      <!-- <swiper ref="mySwiper" :options="swiperOptions"> -->
      <div
        v-for="(item, index) in saleTickets"
        :key="index"
        class="slode-style"
      >
        <div
          class="card-style"
          :class="isSelected(index)?'select-style':'normal-style'"
          @click="selectTick(index)"
        >
          <div class="img-div">
            <img :src="item.ticketPic" alt="">
          </div>
          <!-- {{ '【'+item.projectName+'】'+item.name }} -->
          <div class="card-name">{{ ('【'+item.projectName+'】'+item.name).length > 30 ? ('【'+item.projectName+'】'+item.name).slice(0,27) + '...' : ('【'+item.projectName+'】'+item.name) }}</div>
          <div class="time-style">{{ '【'+item.businessHoursStart+'~'+item.businessHoursEnd+'】' }}</div>
          <div class="card-first-price">售价￥{{ item.price }}</div>
          <div v-if="item.deposit" class="card-two-price">押金￥{{ item.deposit }}</div>
          <div class="num-module-style">
            <div class="sale-style">
              <span class="num-title-style">已售：</span>
              <span class="num-style">{{ item.saleNum }}</span>
            </div>
            <div class="surplus-style">
              <span class="num-title-style">剩余：</span>
              <span class="num-style">{{ item.surplusDay }}</span>
            </div>
          </div>
        </div>
      </div>
      <!-- </swiper> -->
    </div>
    <div class="line-style top-speace" />

    <div class="property-title top-speace">订购门票</div>
    <el-table
      :data="selectedMessage"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column align="center" label="序号" type="index" />
      <el-table-column align="center" label="门票信息" prop="name" min-width="130" show-overflow-tooltip />
      <el-table-column align="center" label="门票价格" prop="price" min-width="100" show-overflow-tooltip />
      <el-table-column align="center" label="押金" prop="deposit" min-width="100" show-overflow-tooltip />
      <el-table-column align="center" label="适用时间" prop="fitDate" min-width="200" show-overflow-tooltip />
      <el-table-column align="center" label="数量">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.count" size="small" :step="1" step-strictly :min="1" :max="Number(scope.row.surplusDay)" label="数量" style="width: 100%" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button v-preventReClick type="text" class="delete-style" @click="toDelete(scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div v-if="selectedMessage.length" class="bottom-style">
      <div class="sum-style">
        <span class="sum-title">合计：</span>
        <span class="sum-value">{{ totalMoney }}</span>
      </div>
      <div v-if="meta.indexOf('TicketOrderAdd') > -1" v-preventReClick class="btn-style" @click="totalAction">去结算</div>
    </div>

    <orderDialog ref="child" @orderSuccess="orderSuccess" />
  </div>
</template>

<script>
import { getProjectList, onSaleTicket, printOrder } from '@/api'
import { parseTime } from '@/utils'
import orderDialog from '@/components/order'

export default {
  name: 'TicketSell',
  components: {
    orderDialog
  },
  data() {
    return {
      meta: [],
      swiperOptions: {
        slidesPerView: 'auto',
        pagination: {
          el: '.swiper-pagination'
        }
      },
      selectProNmae: '',
      date: '',
      saleTickets: [],
      selectedTickets: [],
      selectedMessage: [],
      project: {
        projectList: []
      },
      param: {
        date: parseTime(new Date())
      }
    }
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.$swiper
    },
    totalMoney() {
      let money = 0.0
      this.selectedMessage.forEach(ticket => {
        money = ((Number(ticket.price) + Number(ticket.deposit)) * Number(ticket.count)) + money
      })
      return parseFloat(money).toFixed(2)
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getProList()
    this.getTickets()
  },
  methods: {
    getList() {
      this.$router.push({ name: 'TicketOrder' })
    },
    orderSuccess() {
      this.saleTickets = []
      this.getTickets()
      this.selectedMessage = []
    },
    printTC(id) {
      printOrder({
        param: {
          contentMap: {},
          venueId: sessionStorage.getItem('venueId'),
          workId: id,
          templateCode: 'TICKETTPML'
        }
      })
        .then(res => {
          // console.log(res)
        })
    },
    getProList() {
      getProjectList({
        pagination: {
          page: 1,
          rows: 1000,
          firstQueryTime: parseTime(new Date())
        },
        param: {
          venueId: sessionStorage.getItem('venueId'),
          projectName: '',
          type: '1',
          status: ''
        }
      })
        .then(res => {
          if (res.errorCode !== '000000') return
          this.project.projectList = res.recordList
        })
    },
    conditionChange() {
      this.getTickets()
    },
    getTickets() {
      onSaleTicket({
        param: {
          projectId: this.selectProNmae,
          saleDate: this.param.date === null ? parseTime(new Date()) : this.param.date,
          venueId: sessionStorage.getItem('venueId')
        }
      }).then(res => {
        if (res.errorCode !== '000000') return
        this.saleTickets = res.recordList
        this.selectedTickets = []
        this.selectedMessage.forEach(selectedTicket => {
          const index = this.saleTickets.findIndex(ticket => selectedTicket.id === ticket.id)
          this.selectedTickets.push(index)
        })
      }).catch(err => {
        console.log(err)
      })
    },
    selectTick(index) {
      if (this.selectedTickets.includes(index)) {
        const i = this.selectedTickets.indexOf(index)
        this.selectedTickets.splice(i, 1)
        const tIndex = this.selectedMessage.findIndex(selectedTicket => selectedTicket.id === this.saleTickets[parseInt(index)].id)
        this.selectedMessage.splice(tIndex, 1)
      } else {
        const item = this.saleTickets[index]
        this.selectedTickets.push(index)
        this.selectedMessage.push({
          sort: this.selectedTickets.length,
          id: item.id,
          name: ('【' + item.projectName + '】' + item.name).length > 30 ? ('【' + item.projectName + '】' + item.name).slice(0, 27) + '...' : ('【' + item.projectName + '】' + item.name),
          price: item.price,
          deposit: item.deposit || 0,
          count: 1,
          fitDate: item.saleDate + ' ' + item.businessHoursStart + '~' + item.businessHoursEnd,
          surplusDay: item.surplusDay
        })
      }
    },
    isSelected(index) {
      return this.selectedTickets.includes(index)
    },
    titleSub(title) {
      if (title.length > 30) {
        return title.splice(27, 3, '...')
      }
      return title
    },
    toDelete(index) {
      const deletTicket = this.selectedMessage[index]
      const tempIndex = this.saleTickets.findIndex(ticket => ticket.id === deletTicket.id)
      const selIndex = this.selectedTickets.indexOf(tempIndex)
      this.selectedTickets.splice(selIndex, 1)
      this.selectedMessage.splice(index, 1)
    },
    totalAction() {
      const p = []
      this.selectedMessage.forEach(select => {
        p.push({
          id: select.id,
          num: select.count
        })
      })
      this.$refs.child.continuePay(p, 'ticket')
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';
.data-picker-style {
  margin-left: 20px;
}

.top-speace {
  margin-top: 20px;
}
.saleTickets{
  display: flex;
  width: 100%;
  overflow: auto;
}
.slode-style {
  width: 185px;
  height: 226px;
  margin-right: 20px;
  margin-bottom: 10px;
  cursor: pointer;
}

.delete-style{
  color: $SecondColor;
}

.card-style {
  position: relative;
  // background-color: #000;
  display: inline-block;
  vertical-align: middle;
  width: 185px;
  height: 225px;
  margin-right: 20px;
  .img-div {
    width: 165px;
    height: 75px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 10px;
    margin-left: 10px;
    img {
      height: 100%;
      margin: auto;
      max-width: 100%;
      border-radius: 4px;
    }
  }
  .card-name {
    color: #222;
    font-size: 13px;
    // font-weight: 500;
    margin-top: 15px;
    margin-left: 10px;
    margin-right: 10px;
    padding: 0;
    line-height: 14px;
  }
  .time-style {
    position: absolute;
    // background-color: #f00;
    color: #999;
    font-weight: 500;
    bottom: 70px;
    margin-bottom: 5px;
    margin-left: 10px;
    margin-right: 10px;
    text-align: center;
    font-size: 15px;
    line-height: 17px;
    width: 165px;
    height: 18px;
  }
  .card-first-price {
    position: absolute;
    // background-color: #f00;
    color: $SecondColor;
    bottom: 50px;
    // margin-bottom: 5px;
    margin-left: 10px;
    margin-right: 10px;
    font-size: 15px;
    line-height: 17px;
    width: 165px;
    height: 18px;
    text-align: center;
  }
  .card-two-price {
    position: absolute;
    color: $SecondColor;
    bottom: 30px;
    // margin-bottom: 5px;
    margin-left: 10px;
    margin-right: 10px;
    font-size: 15px;
    line-height: 17px;
    width: 165px;
    height: 18px;
    text-align: center;
  }
  .num-module-style {
    position: absolute;
    // background-color: #f00;
    margin-bottom: 4px;
    bottom: 5px;
    margin-left: 10px;
    margin-right: 10px;
    font-size: 15px;
    line-height: 17px;
    height: 18px;
    width: 165px;
    .sale-style {
      float: left;
    }
    .surplus-style {
      float: right;
    }
    .num-title-style {
      color: #999;
    }
    .num-style {
      color: $SecondColor;
    }
  }
}
.select-style {
  border: solid 3px $primaryColor;
}
.normal-style {
  border: solid 1px #d8d8d8;
}

.bottom-style {
  height: 50px;
  border-left: 1px #DFE2E8 solid;
  border-right: 1px #DFE2E8 solid;
  border-bottom: 1px #DFE2E8 solid;
  .sum-style {
    margin-left: 10px;
    margin-top: 17px;
    height: 15px;
    float: left;
    .sum-title {
      color: #222;
      font-size: 13px;
    }
    .sum-value {
      color: $SecondColor;
      font-size: 17px;
      font-weight: 700;
    }
  }
  .btn-style {
    float: right;
    width: 80px;
    height: 30px;
    margin-right: 10px;
    margin-top: 10px;
    background-color: $primaryColor;
    color: #fff;
    text-align: center;
    border-radius: 4px;
    line-height: 30px;
    font-size: 13px;
    font-weight: 700;
  }
}
.count-style {
  margin-left: 5px;
  margin-right: 5px;
}
.pay-style {
  min-width: 80px;
  padding-left: 5px;
  padding-right: 5px;
  height: 30px;
  font-size: 15px;
  line-height: 1px;
  vertical-align: middle
}
.slelect-pay-style {
  background-color: $primaryColor;
  color: #fff;
}
.normal-pay-style {
  border: 1px solid #d8d8d8;
  background-color: #fff;
  color: #222;
}
.messgae-style {
  color: $SecondColor;
}
.price-style {
  font-size: 17px;
  font-weight: 500;
}
.unit-style {
  margin-right: 20px;
}
.bottom-speace-style {
  margin-bottom: 10px;
}
.card-message {
  margin-bottom: 10px;
  background-color: #f2f2f2;
  height: 60px;
  line-height: 20px;
  .first-line {
    height: 30px;
    padding: 5px;
    .message-title-style {
    color: #999;
    }
    .card-name-style {
      display: inline-block;
      border: 1px #999 solid;
      border-radius: 4px;
      background-color: #fff;
      font-size: 12px;
      padding-left: 5px;
      padding-right: 5px;
      height: 20px;
      text-align: center;
    }
    .close-style {
      font-size: 20px;
      float: right;
      margin-right: 5px;
      margin-top: -2px;
      padding: 0;
      border: 0px #999 solid;
    }
    .message-value-style {
      margin-right: 15px;
      color: #222;
    }
  }
}
.footer-style {
  display: flex;
  justify-content: space-between;
  background-color: #f2f2f2;
  margin: -10px -20px -30px;
  height: 50px;
  .bottom-left {
    margin-left: 20px;
    height: 20px;
    margin-top: 15px;
    .sum-title-style {
      font-size: 15px;
      color: #222;
    }
    .sum-pay-value-style {
      font-size: 17px;
      font-weight: 500;
      color: $SecondColor;
    }
    .dis-title-style {
      font-size: 14px;
      color: #222;
    }
    .discoun-style {
      font-size: 16px;
      color: $SecondColor;
    }
  }
  .bottom-right {
    margin-right: 20px;
    margin-top: 10px;
    height: 30px;
    .pay-action-style {
      width: 100px;
      height: 30px;
      font-size: 15px;
      line-height: 1px;
      vertical-align: middle
    }
  }
}

</style>
